import java.util.ArrayList;
import java.util.List;

public class Test {
    int left = 0, right = 0, n = 0;
    StringBuffer path = new StringBuffer();
    List<String> list = new ArrayList<>();
    public List<String> generateParenthesis(int _n) {
        n = _n;
        dfs();
        return list;
    }
    void dfs() {
        if(right == n) {
            list.add(path.toString());
            return;
        }
        if(left < n) {
            path.append('(');
            left++;
            dfs();
            path.deleteCharAt(path.length() - 1);
            left--;
        }
        if(left < right) {
            path.append(')');
            right++;
            dfs();
            path.deleteCharAt(path.length() - 1);
            right--;
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.generateParenthesis(3);
        System.out.println(test.list);
    }
}
